from fastapi import FastAPI, HTTPException, Header
from pydantic import BaseModel
from typing import Optional, Dict
from api_client import QPetAPIClient

app = FastAPI(
    title="Q宠大乐斗API",
    description="Q宠大乐斗API接口服务",
    version="1.0.0"
)

# 存储客户端实例
client = None

class DailyGiftRequest(BaseModel):
    cmd: str
    op: str
    key: str

@app.on_event("startup")
async def startup_event():
    global client
    # 初始化API客户端
    cookie = "pgv_pvid=1200892164; RK=vQuIYh4+2u; ptcz=5e943d7ee02f1202f85138bee12269a6af0bdbaec95a3af3bf0ef98eab5a7a32; ts_uid=215004682; ptui_loginuin=50595531; pgv_info=ssid=s5345542696; _qpsvr_localtk=0.6299769144763538; uin=o0050595531; skey=; gameapi_access_token=AE18FD9DC185AAA503A6DFFE3E982C7F; qqgame_nick=%E8%AE%B0%E5%BF%86; chrometag=1; browsertag=10; authorization_code=306032A7908A2B0DF1BE7CCA9D863ECD; wherefrom=4; frompage=1; ts_refer=graph.qq.com/oauth2.0/show%3Fwhich%3DLogin%26display%3Dpc%26response_type%3Dcode%26client_id%3D102067279%26redirect_uri%3Dhttps%3A//fight.pet.qq.com/g; accessToken=540081F8890C5D37E901FEBB84B13471; openId=E5451D04AC68FE85CCF6C8E5BF765BBA; newuin=50595531; outflow=1; kaipingusertype=0; secretInfo=1; ts_last=fight.pet.qq.com/vip.html"
    client = QPetAPIClient(cookie)

@app.get("/")
async def root():
    return {"message": "Q宠大乐斗API服务已启动"}

@app.post("/api/daily_gift")
async def get_daily_gift(request: DailyGiftRequest):
    """
    获取每日礼包
    
    Args:
        request: 包含cmd、op和key的请求体
        
    Returns:
        API响应结果
    """
    try:
        response = client.get_daily_gift(
            cmd=request.cmd,
            op=request.op,
            key=request.key
        )
        return response
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000) 